package com.omesoft.hksnore;

import android.os.Bundle;
import android.os.StrictMode;
import android.support.v4.app.Fragment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;

import com.omesoft.hksnore.me.MeFragment;
import com.omesoft.hksnore.monitoring.MonitorFragment;
import com.omesoft.hksnore.report.ReportFragment;
import com.omesoft.hksnore.util.myactivity.MyFragmentActivity;
import com.omesoft.hksnore.util.myactivity.MyFragmentPagerAdapter;
import com.omesoft.hksnore.util.omeview.CustomViewPager;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;

/**
 * 主界面
 *
 * @autor omesoft_lgc
 * <p>
 * create at 2017/2/16 15:21
 */
public class MainActivity extends MyFragmentActivity implements OnClickListener {

    private final static String TAG = "MainActivity";
    private CustomViewPager mViewPager;
    private ArrayList<Fragment> fragments;
    private LinearLayout snore_main_monitor, snore_main_report, snore_main_mine;
    public static int Position = 0;
    private MyFragmentPagerAdapter fragmentPagerAdapter;
    private int fragmentSize = 3;
    private final int POIONTONE = 0;
    private final int POIONTTWO = 1;
    private final int POIONTTHREE = 2;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.snore_activity_main);
        if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }
        initView();
        initViewPager();
        initHandler();
    }

    @Override
    protected void initView() {
        mViewPager = (CustomViewPager) findViewById(R.id.my_pager);
        mViewPager.setOffscreenPageLimit(3);
        snore_main_monitor = (LinearLayout) findViewById(R.id.snore_main_monitor);
        snore_main_monitor.setOnClickListener(this);
        snore_main_report = (LinearLayout) findViewById(R.id.snore_main_report);
        snore_main_report.setOnClickListener(this);
        snore_main_mine = (LinearLayout) findViewById(R.id.snore_main_mine);
        snore_main_mine.setOnClickListener(this);
        snore_main_monitor.setSelected(true);

    }

    /**
     * 应该延迟加载，进来就加载需要显示的那个碎片，其他碎片等到用户点击了再初始化
     */
    private void initViewPager() {
        fragments = new ArrayList<Fragment>();
        for (int i = 0; i < fragmentSize; i++) {
            switch (i) {
                case POIONTONE:
                    MonitorFragment monitorFragment = new MonitorFragment();
                    if (Position != POIONTONE) {
                        monitorFragment.setUserVisibleHint(false);// 在这里实现Fragment数据的缓加载.
                    }
                    fragments.add(monitorFragment);
                    break;
                case POIONTTWO:
                    ReportFragment reportFragment = new ReportFragment();
                    if (Position != POIONTTWO) {
                        reportFragment.setUserVisibleHint(false);// 在这里实现Fragment数据的缓加载.
                    }
                    fragments.add(reportFragment);

                    break;
                case POIONTTHREE:
                    MeFragment meFragment = new MeFragment();
                    if (Position != POIONTTHREE) {
                        meFragment.setUserVisibleHint(false);// 在这里实现Fragment数据的缓加载.
                    }
                    fragments.add(meFragment);
                    break;
                default:
                    break;
            }
        }
        fragmentPagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager(), fragments);
        mViewPager.setAdapter(fragmentPagerAdapter);
        fragmentPagerAdapter.setFragments(fragments);
        setSelectedLin(Position);
        mViewPager.setCurrentItem(Position, false);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.snore_main_monitor:
                Position = POIONTONE;
                break;
            case R.id.snore_main_report:
                Position = POIONTTWO;
                break;
            case R.id.snore_main_mine:
                Position = POIONTTHREE;
                break;
            default:
                break;
        }
        setSelectedLin(Position);
        mViewPager.setCurrentItem(Position, false);
    }

    //在fragment中调用父容器的方法，进行切换操作
    public void changePosition() {
        Position = POIONTTWO;
        setSelectedLin(Position);
        mViewPager.setCurrentItem(Position, false);
    }


    /**
     * 处理下面的导航按钮。
     *
     * @param position
     */
    public void setSelectedLin(int position) {
        snore_main_monitor.setSelected(false);
        snore_main_report.setSelected(false);
        snore_main_mine.setSelected(false);
        switch (position) {
            case 0:
                snore_main_monitor.setSelected(true);
                break;
            case 1:
                snore_main_report.setSelected(true);
                break;
            case 2:
                snore_main_mine.setSelected(true);
                break;
            default:
                break;
        }
    }

    @Override
    public void finish() {
        super.finish();
        Position = 0;
    }


    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(context);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(context);
    }
}
